<?php
namespace app\repair\model;
use app\common\model\Common;
use think\Db;
class Goods extends Common
{
	protected $table = 'mps_goods';
	protected $append = ['category_name','cate_name','number'];
	
	/**
	 * 减少维修配件库存
	 * @param [type] $goods     [description]
	 * @param [type] $repair_id [description]
	 */
	public function cutGoodsNumber($goods)
	{
		foreach($goods as $k => $v){
			$num = Db::name('stock')->where('goods_id',$v['goods_id'])->value('number');
			//判断维修配件库存
			if($num < $v['num']){
				return $v['goods_name'];
			}
			Db::name('stock')->where('goods_id',$v['goods_id'])->setDec('number',$v['num']);
		}
	}
	/**
	 * 获取所有维修配件商品列表
	 * @param array $where 查询条件
	 * @param string $order 排序条件
	 * @param intger $page 当前页数
	 * @param intger $row 每页条数
	 * @param string $search 关键字检索条件
	 */
	public function listData($where, $order, $page, $row, $search = '', $ty = null)
	{
		if($where == false) return [];
		// $this->append = ['category_name','cate_name'];
		if($ty == 'edit'){
			return self::stockData($where, $order, $page, $row, $search);
		}
		return self::allData($where, $order, $page, $row, $search);
	}
	/**
	* 商品为纬度
	* 获取所有维修配件
	*/
	private function allData($where, $order, $page, $row, $search = '')
	{
		if( $row == 'limit' ){
			return $this->alias('g')
			->join('mps_stock s','g.goods_id = s.goods_id','LEFT')
			->field('g.*,s.buying_price,s.number,s.position')
			->where('s.state',0)->where('s.is_show',0)->where('s.headquarters',0)
			->where($search)->where($where)->order($order)->group('g.goods_id')->select();
		}
		return $this->alias('g')
		->join('mps_stock s','g.goods_id = s.goods_id','LEFT')
		->field('g.*,s.buying_price,s.number,s.position')
		->where('s.state',0)->where('s.is_show',0)->where('s.headquarters',0)
		->where($search)->where($where)->order($order)->group('g.goods_id')->page($page, $row)->select();
	}
	/**
	* 库存批次为纬度
	* 获取所有维修配件
	*/
	private function stockData($where, $order, $page, $row, $search = '')
	{
		if( $row == 'limit' ){
			return $this->alias('g')
			->join('mps_stock s','g.goods_id = s.goods_id','RIGHT')
			->field('g.*,s.buying_price,s.number,s.position,s.stock_id')
			->where('s.state',0)->where('s.is_show',0)->where('s.headquarters',0)
			->where($search)->where($where)->order($order)->group('s.stock_id')->select();
		}
		return $this->alias('g')
		->join('mps_stock s','g.goods_id = s.goods_id','RIGHT')
		->field('g.*,s.buying_price,s.number,s.position,s.stock_id')
		->where('s.state',0)->where('s.is_show',0)->where('s.headquarters',0)
		->where($search)->where($where)->order($order)->group('s.stock_id')->page($page, $row)->select();
	}
	/**
	 * 获取所有维修配件商品总条数
	 * @param array $where 查询条件
	 * @param string $order 排序条件
	 * @param string $search 关键字检索条件
	 */
	public function listCount($where, $order, $search = '', $ty = null)
	{
		if($where == false) return 0;
		if($ty == 'edit'){
			return self::stockDataCount($where, $order, $search);
		}
		return self::allDataCount($where, $order, $search);
	}
	/* 商品为纬度
	* 获取所有维修配件
	*/
	private function allDataCount($where, $order, $search)
	{
		$count = $this->alias('g')
		->join('mps_stock s','g.goods_id = s.goods_id','LEFT')
		->where($search)->where($where)
		->where('s.state',0)->where('s.is_show',0)->where('s.headquarters',0)
		->order($order)->group('g.goods_id')->count();
		return $count;
	}
	/**
	* 库存批次为纬度
	* 获取所有维修配件
	*/
	private function stockDataCount($where, $order, $search)
	{
		$count = $this->alias('g')
		->join('mps_stock s','g.goods_id = s.goods_id','RIGHT')
		->where($search)->where($where)
		->where('s.state',0)->where('s.is_show',0)->where('s.headquarters',0)
		->order($order)->group('s.stock_id')->count();
		return $count;
	}
	/**
	 * 维修配件子分类集合
	 */
	public function getRepairCat()
	{
		return Db::name('category')->where('fid',config('parameter.repair_cat_id'))->select();
	}
	/**
	 * 获取器
	 */
	public function getCategoryNameAttr($value, $data)
	{
		return Db::name('category')->where('category_id',$data['category_id'])->value('category_name');
	}
	public function getCateNameAttr($value, $data)
	{
		return Db::name('category')->where('category_id',$data['cat_id'])->value('category_name');
	}
	public function getNumberAttr($value, $data){
		if(isset($data['stock_id']) && $data['stock_id'] > 0) return $value;
		return Db::name('stock')->where('goods_id',$data['goods_id'])->where('is_show',0)->where('state',0)->where('headquarters',0)->sum('number');
	}
	/************************************ 私有 *****************************************/

}